﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace DataMgr.File
{
    class BdfRecord
    {
        SingleHisDfData m_Record = new SingleHisDfData();
        BmsHisData m_bmsInfo = new BmsHisData();
        int m_nOneRdSize = 0;
        Byte m_btHaveBms = 0;

        public void SetParam(Byte[] btBuf)
        {
            m_nOneRdSize = btBuf.Length;
            int baseSize = Marshal.SizeOf(m_Record);
            if (m_nOneRdSize == baseSize)
            {
                m_Record = (SingleHisDfData)DaFileElement.RawDeserialize(btBuf, typeof(SingleHisDfData));
            }
            else if (1 == btBuf[baseSize])
            {
                m_btHaveBms = 1;

                m_bmsInfo.StDataCount = BitConverter.ToUInt16(btBuf, baseSize+1);
                m_bmsInfo.VolCount = BitConverter.ToUInt16(btBuf, baseSize + 3);
                m_bmsInfo.TempCount = BitConverter.ToUInt16(btBuf, baseSize + 5);
                m_bmsInfo.SelfCount = BitConverter.ToUInt16(btBuf, baseSize + 7);
                m_nOneRdSize += 9;
                int pos = baseSize + 9;
                if (m_bmsInfo.StDataCount > 0)
                {
                    int nLen = m_bmsInfo.StDataCount * Marshal.SizeOf(typeof(DInfo));
                    m_bmsInfo.StData = new byte[nLen];
                    Array.Copy(btBuf, pos, m_bmsInfo.StData,0, nLen);
                    pos += nLen;
                    m_nOneRdSize += nLen;
                }

                if (m_bmsInfo.VolCount > 0)
                {
                    int nLen = m_bmsInfo.VolCount * Marshal.SizeOf(typeof(SingleInfo));
                    m_bmsInfo.VolData = new byte[nLen];
                    Array.Copy(btBuf, pos, m_bmsInfo.VolData, 0, nLen);
                    pos += nLen;
                    m_nOneRdSize += nLen;
                }

                if (m_bmsInfo.TempCount > 0)
                {
                    int nLen = m_bmsInfo.TempCount * Marshal.SizeOf(typeof(SingleInfo));
                    m_bmsInfo.TempData = new byte[nLen];
                    Array.Copy(btBuf, pos, m_bmsInfo.TempData, 0, nLen);
                    pos += nLen;
                    m_nOneRdSize += nLen;
                }

                if (m_bmsInfo.SelfCount > 0)
                {
                    int nLen = m_bmsInfo.SelfCount * Marshal.SizeOf(typeof(DInfo));
                    m_bmsInfo.SelfData = new byte[nLen];
                    Array.Copy(btBuf, pos, m_bmsInfo.SelfData, 0, nLen);
                    m_nOneRdSize += nLen;
                }
            }
        }

        public UInt32 GetRecordID()//得到记录序号 0~n
        {
            return m_Record.dwTestSn;
        }
        public double GetCur()      //得到电流 /V
        {
            return m_Record.nCurr * DaFileElement.SCALE_IU;
        }
        public double GetVol()      //得到电压 /v
        {
            return m_Record.nVolt * DaFileElement.SCALE_IU;
        }
        public double GetCap()      //得到容量 /Ah
        {
            return m_Record.nCap * DaFileElement.SCALE_CN;
        }
        public double GetPow()      //得到功率
        {
            if (Math.Abs(GetVol()) < DaFileElement.SCALE_IU || Math.Abs(GetCur()) < DaFileElement.SCALE_IU)
            {
                return 0;
            }
            else
                return GetVol() * GetCur();
        }
        public double GetEng()     //得到能量 /Wh
        {
            return m_Record.nEngSum * DaFileElement.SCALE_CN;
        }
        public double GetIR()        //得到内阻 /mOhm
        {
            return m_Record.nInterRes * DaFileElement.SCALE_TR;
        }
        public double GetTemp()   //得到温度 /℃
        {
            return m_Record.nTempature * DaFileElement.SCALE_TR;
        }
        public UInt64 GetTime()    //得到时间,/ms,记录时间,流程开始后的毫秒数
        {
            return m_Record.recordTime > 0 ? m_Record.recordTime : 0;
        }
        public UInt64 GetTimeOffStep()   //工步时间,当前工步执行的毫秒数
        {
            return m_Record.stepTime;
        }
        public UInt64 GetLocalTime()  //得到绝对时间
        {
            return (UInt64)m_Record.dTestTime;
        }

        public Byte GetStepType()
        {
            return m_Record.btStepType;
        }
        public UInt32 GetStepID()
        {
            return m_Record.stepSeq;
        }
        public UInt32 GetStepNo()
        {
            return m_Record.stepID;
        }
        public UInt32 GetCycleID()
        {
            return m_Record.dwCycle;
        }
        //public void CopyTo(CNdaXRecord& newRecord);

        public bool SelfCheck(UInt32 dwPreRecordId )
        {
            if (GetRecordID() <= 0 || GetStepType() == 0 ||
    //(GetStepType() >= stStepCount && GetStepType() != STEP_MODE_RECYCLE) ||
                dwPreRecordId == GetRecordID())
            {
                return false;
            }

            return true;
        }
        //public void Clear();

        public BmsHisData GetBms()
        {
            return m_bmsInfo;
        }
        public Byte GetBmsFlag()
        {
            return m_btHaveBms;
        }
    }
}
